package com.ict.txmsv2.controllerAdvice;

import com.ict.txmsv2.exception.BadFileTypeException;
import com.ict.txmsv2.model.RespBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;

/**
 * @author yoleen
 * @date 2020/2/27 14:34
 */

@RestControllerAdvice
@Slf4j
public class GolbalExceptionHandler {
    @ExceptionHandler(SQLException.class)
    public RespBean sqlException(SQLException e){
        System.out.println("cause:"+e.getCause());
        System.out.println("message:"+e.getMessage());
        if (e instanceof SQLIntegrityConstraintViolationException){
            return RespBean.error("此数据有关联数据，操作失败");
        }
        return RespBean.error("数据库异常，操作失败");
    }

    @ExceptionHandler(BadFileTypeException.class)
    public RespBean badFileType(BadFileTypeException e) {
        log.error("文件类型错误!");
        return RespBean.error("文件类型错误!");
    }
}
